//
//  SubProjectDetailController.swift
//  QualityControlSystem
//
//  Created by Daniel Hu on 1/11/17.
//  Copyright © 2017 com.mask. All rights reserved.
//

import UIKit

class SubProjectDetailController: UITableViewController {
    
    var data = [SubProjectProcess]()
    var projectId: String?
    var id: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.backgroundColor = Color.bgGray
        tableView.register(SubProjectInfoCell.self, forCellReuseIdentifier: "foo")
        if User.shared.role == .inspector {
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "质检报告", style: .plain, target: self, action: #selector(qualityReport))
        } else {
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "上传整体照片", style: .plain, target: self, action: #selector(uploadPictures))
        }
        loadData()
    }
    
    init(projectId: String?, id: String?) {
        self.projectId = projectId
        self.id = id
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func loadData() {
        HTTPRequest.subProjectProcessWith(subId: id!, completion: { response in
            self.data = response!
            self.tableView.reloadData()
        })
    }
    
    func qualityReport() {
        let qualityReportVC = QualityReportController(style: .grouped)
        navigationController?.pushViewController(qualityReportVC, animated: true)
    }
    
    func uploadPictures() {
        
    }
    
    //MARK: - UITableViewDataSource
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "foo", for: indexPath) as? SubProjectInfoCell
        cell?.data = data[indexPath.section]
        return cell!
    }
    
    //MARK: - UITableViewDelegate
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return Constant.subProjectCellHeight
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView()
        return header
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let process = data[indexPath.section]
        if let status = process.status {
            switch status {
            case .upload:
                if User.shared.role == .inspector {
                    let vc = HistoryPicListController(style: .plain, processId: process.id)
                    navigationController?.pushViewController(vc, animated: true)
                } else {
                    let vc = ProcessPictureController(collectionViewLayout: UICollectionViewFlowLayout(), status: .upload, projectId: projectId!, subProjectId: id!, processId: process.id)
                    vc.hidesBottomBarWhenPushed = true
                    navigationController?.pushViewController(vc, animated: true)
                }
                break
            case .review:
                let vc = ProcessPictureController(collectionViewLayout: UICollectionViewFlowLayout(), status: .review, projectId: projectId, subProjectId: id, processId: process.id)
                vc.hidesBottomBarWhenPushed = true
                navigationController?.pushViewController(vc, animated: true)
                break
            case .reform:
                if User.shared.role == .inspector {
                    let vc = HistoryPicListController(style: .plain, processId: process.id)
                    navigationController?.pushViewController(vc, animated: true)
                } else {
                    let vc = ProcessPictureController(collectionViewLayout: UICollectionViewFlowLayout(), status: .reform, projectId: projectId!, subProjectId: id!, processId: "")
                    vc.hidesBottomBarWhenPushed = true
                    navigationController?.pushViewController(vc, animated: true)
                }
                break
            case .pass:
                //添加图片
//                let vc = SKPhotoBrowser()
//                present(vc, animated: true, completion: nil)
                break
            }
        }
    }
}
